package com.cloud.wms.ui.inside.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.inside.contract.ContainerReleaseContact;
import com.cloud.wms.ui.sort.entity.DetachBoxBean;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;

/**
 * Created by FC on 2021/3/10.
 * Desc：容器释放
 */
public class ContainerReleasePresenter extends BaseRxPresenter<ContainerReleaseContact.View> implements ContainerReleaseContact.Presenter {
    public static final int CONTAINER_RELEASE_SUCCESS = 0x110;

    @Inject
    public ContainerReleasePresenter() {

    }

    @Override
    public void containerRelease(String containerCode) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        params.put("BoxCode", containerCode);
        FineExApplication
                .component()
                .http()
                .getContainerMsg(params)
                .map(new ResponseWrapper<>())
                .flatMap(new Function<DetachBoxBean, ObservableSource<Boolean>>() {
                    @Override
                    public ObservableSource<Boolean> apply(DetachBoxBean detachBoxBean) throws Exception {
                        List<Long> boxBeanList = new ArrayList<>();
                        boxBeanList.add(detachBoxBean.getContainerID());
                        Params params = Params.newInstance(Params.Type.WAREHOUSE);
                        params.put("lstContainerID", boxBeanList);
                        params.put("userID", FineExApplication.component().loginCache().getUserInfo().getUserID());
                        return FineExApplication
                                .component()
                                .http()
                                .containerRelease(params)
                                .map(new ResponseWrapper<>());
                    }
                })
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Boolean>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Boolean b) {
                        mView.onSuccess(MessageCreator.createMessage(b, CONTAINER_RELEASE_SUCCESS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
